package com.cian.service.impl;

import com.cian.entity.Subsystem;
import com.cian.entity.dto.SubsystemForStockDTO;
import com.cian.entity.dto.WebLinkDTO;
import com.cian.mapper.SubsystemMapper;
import com.cian.service.ISubsystemService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cian.utils.CreateCodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Cian
 * @since 2021-03-04
 */
@Service
public class SubsystemServiceImpl extends ServiceImpl<SubsystemMapper, Subsystem> implements ISubsystemService {
    @Autowired
    SubsystemMapper subsystemMapper;

    /**
     * 像服务器中的子服务器表中添加项目，生成并返回该子服务器在数据库对应的识别码
     * @param subsystem 子服务器对象
     * @return 返回该子服务器在数据库对应的识别码
     */
    @Override
    public String createSubSystem(Subsystem subsystem, CreateCodeUtils methods) {
        // 生成识别码
        String licenseCode = methods.createCode();
        subsystem.setSCode(licenseCode);
        subsystemMapper.insert(subsystem);
        return licenseCode;
    }

    @Override
    public List<SubsystemForStockDTO> selectByServeForStock(String serveAccount) {
        return subsystemMapper.selectByServeForStock(serveAccount);
    }

    @Override
    public Subsystem selectByServerCode(String serverCode) {
        Map<String, Object> map = new HashMap<>();
        map.put("s_code", serverCode);
        return (subsystemMapper.selectByMap(map)).get(0);
    }

    @Override
    public List<WebLinkDTO> selectByServeAccount(String serveAccount) {
        return subsystemMapper.selectByServeAccount(serveAccount);
    }
}
